package com.xinxing.learning.crypto.util;

import cn.hutool.crypto.symmetric.AES;
import com.xinxing.learning.crypto.keyresolver.InterfaceKeyresolver;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class AESUtil {

    private static AES aes;

    private final Map<Class<? extends InterfaceKeyresolver>, InterfaceKeyresolver> keyresolverMap;

    @Resource
    private AES iocAes;

    public AESUtil(List<? extends InterfaceKeyresolver> keyresolvers) {
        this.keyresolverMap = keyresolvers.stream().collect(
                Collectors.toMap(interfaceKeyresolver -> interfaceKeyresolver.getClass(), interfaceKeyresolver -> interfaceKeyresolver)
        );
    }

    public InterfaceKeyresolver getInterfacekeyresolver(Class clazz){
        return keyresolverMap.get(clazz);
    }

    @PostConstruct
    public void setAes() {
        AESUtil.aes = this.iocAes;
    }

    public static AES getAes() {
        return aes;
    }

    public static String encryptHex(byte[] data) {
        return aes.encryptHex(data);
    }

    public static String encryptHex(InputStream data) {
        return aes.encryptHex(data);
    }

    public static String encryptHex(String data) {
        return aes.encryptHex(data, StandardCharsets.UTF_8);
    }

    /**
     * 解密
     */

    public static String decrypt(byte[] data) {
        return aes.decryptStr(data, StandardCharsets.UTF_8);
    }

    public static String decrypt(InputStream data) {
        return aes.decryptStr(data);
    }

    public static String decrypt(String data) {
        return aes.decryptStr(data, StandardCharsets.UTF_8);
    }

}